require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/tf_icebox_chillet.zip"),
}

local prefabs =
{
    "collapse_small",
}

local function onopen(inst)
    inst.AnimState:PlayAnimation("open")
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_open")
end

local function onclose(inst)
    inst.AnimState:PlayAnimation("close")
    inst.AnimState:PushAnimation("closed")
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_close")
end

local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    inst.components.container:DropEverything()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("metal")
    inst:Remove()
end

local function onhit(inst, worker)
    inst.AnimState:PlayAnimation("hit")
    inst.components.container:DropEverything()
    inst.AnimState:PushAnimation("closed", true)
    inst.components.container:Close()
end

local function onbuilt(inst)
    inst.AnimState:PlayAnimation("place")
    inst.AnimState:PushAnimation("closed", true)
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_craft")
end

local function SetPerishRate(inst, item)
    if item == nil then
        return TUNING.TICKF
    end
    if item:HasTag("frozen") and TUNING.TICKF > 0 then
        return 0
    end
    return TUNING.TICKF
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst.MiniMapEntity:SetIcon("tf_icebox_chillet.tex")

    inst:AddTag("fridge") --这个标签可以给暖石降温，但是不能让冰块永久保鲜
    inst:AddTag("structure")

    inst.AnimState:SetBank("tf_icebox_chillet")
    inst.AnimState:SetBuild("tf_icebox_chillet")
    inst.AnimState:PlayAnimation("closed")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
    inst:AddComponent("container")
    inst.components.container:WidgetSetup("tf_icebox_chillet")
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose
    inst.components.container.skipclosesnd = true
    inst.components.container.skipopensnd = true
    inst.components.container:EnableInfiniteStackSize(true) --启用无限堆叠

    -- 设置保鲜倍率
    inst:AddComponent("preserver")
    inst.components.preserver:SetPerishRateMultiplier(SetPerishRate)

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(4)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)

    inst:ListenForEvent("onbuilt", onbuilt)

    AddHauntableDropItemOrWork(inst)

    return inst
end

return Prefab("tf_icebox_chillet", fn, assets, prefabs),
    MakePlacer("tf_icebox_chillet_placer", "tf_icebox_chillet", "tf_icebox_chillet", "closed")
